//存储器封装

/**
 * 存储数据
 * @param key 
 * @param value 
 */
export const setStorage = (key: string, value: object) => {
  try {
    wx.setStorageSync(key, value)
  } catch (error) {
    console.error(`存储指定${key}数据发生异常`, error)
  }
}
/**
 * 获取存储数据
 * @param key 
 */
export const getStorage = (key: string) => {
  try {
    const value = wx.getStorageSync(key)
    if (value) {
      return value;
    }
  } catch (error) {
    console.error(`得到指定${key}数据发生异常`, error)
  }
}
/**
 * 移除存储
 * @param key 
 */
export const removeStorage = (key: string) => {
  try {
    wx.removeStorageSync(key)
  } catch (error) {
    console.error(`移除指定${key}数据发生异常`, error)
  }
}
/**
 * 清除所有数据
 */
export const clearAll = () => {
  try {
    wx.clearStorageSync()
  } catch (error) {
    console.error(`清除所有数据发生异常`, error)
  }
}
/**
 * 异步将数据存储到本地
 * @param key 
 * @param value 
 */
export const asyncSetStorage = (key: string, value: any) => {
  return new Promise((resolve) => {
    wx.setStorage({
      key,
      value,
      complete(res) {
        resolve(res)
      }
    })
  })
}


/**
 * 异步将数据读取
 * @param key 
 * @param value 
 */
export const asyncGetStorage = (key: string) => {
  return new Promise((resolve) => {
    wx.getStorage({
      key,
      complete(res) {
        resolve(res)
      }
    })
  })
}

/**
 * 异步将数据移除
 * @param key 
 * @param value 
 */
export const asyncRemoveStorage = (key: string) => {
  return new Promise((resolve) => {
    wx.removeStorage({
      key,
      complete(res) {
        resolve(res)
      }
    })
  })
}

/**
 * 异步将数据全部移除
 * @param key 
 * @param value 
 */
export const asyncClearStorage = () => {
  return new Promise((resolve) => {
    wx.clearStorage({
      complete(res) {
        resolve(res)
      }
    })
  })
}